<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addDialog({type:1,pid:''})">添加权限</el-button>
        </div>
        <el-table border :data="permissionList" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addDialog({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermissrow(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹窗 -->
    <el-dialog ref="addPermission" :visible="showDialog" title="弹层标题" @close="closeDialog">
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail } from '@/api/permission'
import transTree from '@/utils/transTree'
export default {
  name: 'Permission',
  data() {
    return {
      permissionList: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型 一级或二级
        pid: '' // 添加到哪个节点下
      }
    }
  },
  mounted() {
    this.fetchList()
  },
  methods: {
    async fetchList() {
      const res = await getPermissionList()
      this.permissionList = transTree(res)
    },
    // 删除
    delPermissrow(id) {
      this.$confirm('确定要删除该权限吗?', '温馨提示').then(async() => {
        // 调删除接口
        await delPermission(id)
        // 刷新列表
        this.fetchList()
        // 提示用户
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message.warning('默认数据不可删除！')
      })
    },
    // 双重关闭
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型 区分当前是添加一级权限点还是二级权限点
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加
    addDialog({ type, pid }) {
      this.showDialog = true
      this.formData.pid = pid
      this.formData.type = type
    },
    // 提交
    async confirm() {
      await addPermission(this.formData)
      this.closeDialog()
      this.fetchList()
    },
    // 编辑
    async editPermission(id) {
      // 打开弹窗
      this.showDialog = true
      // 调接口 回填数据
      const res = await getPermissionDetail(id)
      this.$refs.formData = res
    }
  }
}
</script>
